/**
 * 获取多级联动的商品分类
 */
function get_category(id,next,select_id,callback){
    url = THINK.U('Home/api/getcategory','parent_id=' + id );

    var url = url ; 
    $.ajax({
        type : "GET",
        url  : url,
        error: function(request) {
            alert("服务器繁忙, 请联系管理员!");
            return;
        },
        success: function(v) {
			v = "<option value='0'>请选择商品分类</option>" + v;
            $('#'+next).empty().html(v);
			(select_id > 0) && $('#'+next).val(select_id);//默认选中
            if(typeof callback == 'function'){
                callback();
            }
        }
    });
}



// 修改指定表的指定字段值
function changeTableVal(table,id_name,id_value,field,obj)
{
        var src = "";
         if($(obj).attr('src').indexOf("cancel.png") > 0 )
         {          
                src = '/Public/images/yes.png';
                var value = 1;
                
         }else{                    
                src = '/Public/images/cancel.png';
                var value = 0;
         }       
         var url = "/index.php?m=Admin&c=Index&a=changeTableVal&table="+table+"&id_name="+id_name+"&id_value="+id_value+"&field="+field+'&value='+value ;                                    
        $.ajax({
                url:   url,     
                success: function(data){                                    
                    $(obj).attr('src',src);           
                }
        });     
}


// 修改指定表的排序字段
function updateSort(table,id_name,id_value,field,obj)
{              
        var value = $(obj).val();
        $.ajax({
                url:"/index.php?m=Admin&c=Index&a=changeTableVal&table="+table+"&id_name="+id_name+"&id_value="+id_value+"&field="+field+'&value='+value,           
                success: function(data){                                    
                    layer.msg('更新成功', {icon: 1});   
                }
        });     
}
 

/**
    发送短信验证码全局接口 
    需加载jquery.js layer.js
*/
function sendMessage(mobile , flag , callback){
    if(!flag){
        flag = 1;
    }

    var preg = /^1[3|4|5|6|7|8|9]\d{9}$/i;
    if(!preg.test(mobile)){
        layer.msg('手机号格式填写错误');
        return false ;
    }
     url = ThinkPHP.U('Home/Public/sms','flag=' + flag);
     $.post(url , {mobile:mobile} , function(data){
        if(data.status){
             if(typeof callback == 'function'){
                callback();
             }
        }
        layer.msg(data.info);
       
     },'json');

}

/** 封装layer.msg * */
function msg($msg , time){
    time = time ? time : 1500;

    layer.msg($msg , {time:time});
}


 /**
 * addcart 将商品加入购物车
 * @goods_id  商品id
 * @num   商品数量
 * @form_id  商品详情页所在的 form表单
 * @to_catr 加入购物车后再跳转到 购物车页面 默认不跳转 1 为跳转
 */
function AjaxAddCart(goods_id,num,to_catr)
{                       
        to_catr = to_catr == 1 ? 1 : 0 ;
    console.log(to_catr);
        $.ajax({
            type : "POST",
            url:"/Cart/ajaxAddCart",
            data :{goods_id:goods_id,goods_num:num} ,
            dataType:'json',
            success: function(data){  
                if(data.status && to_catr == 1){
                     msg(data.info);
                }else if(data.status && to_catr == 0){
                     location.href = data.url ;
                }else{
                     layer.msg(data.info);
                }                              
            }
    });          
}